<?php
/*
Plugin Name: WP Custom Feed
Plugin URI: http://joshuascott.net/plugin/wpcustomfeed
Description: This plugin outputs a custom RSS feed
Author: Joshua Scott
Version: 0.4
Author URI: http://joshuascott.net/
*/

/*
Version Control 
$Id: wpcustomfeed.php 15 2009-08-10 23:36:39Z joshuascott94 $
$LastChangedDate: 2009-08-10 23:36:39 +0000 (Mon, 10 Aug 2009) $
$LastChangedRevision: 15 $
$LastChangedBy: joshuascott94 $
$HeadURL: http://wpcustomfeed.googlecode.com/svn/ver0_1/wpcustomfeeds/wpcustomfeed.php $
*/
// Create the XML/RSS feed.
function wp_custom_feed_create_feed() {
	global $_opt_custom_feed_field;
    header('Content-Type: text/xml; charset=' . get_option('blog_charset'), true);
    $more = 1;
?>
<?php echo '<?xml version="1.0" encoding="'.get_option('blog_charset').'"?'.'>'; ?>
<?php the_generator( 'comment' ); ?>
<rss version="0.92">
    <channel>
        <title><?php bloginfo_rss('name'); wp_title_rss(); ?></title>
        <link><?php bloginfo_rss('url') ?></link>
        <description><?php bloginfo_rss('description') ?></description>
        <lastBuildDate><?php echo mysql2date('D, d M Y H:i:s +0000', get_lastpostmodified('GMT'), false); ?></lastBuildDate>
        <docs>http://backend.userland.com/rss092</docs>
        <language><?php echo get_option('rss_language'); ?></language>
            <?php do_action('rss_head'); ?>

            <?php if ( have_posts() ) : while (have_posts()) : the_post(); // Starting the WP Loop?>
        <item>
            <title><?php the_title_rss() ?></title>
                        <?php if (get_option('rss_use_excerpt')) { ?>
            <description><![CDATA[<?php the_excerpt_rss() ?>]]></description>
                        <?php } else { // use content ?>
            <description><?php the_content_rss('', 0, '', get_option('rss_excerpt_length')) ?></description>
                        <?php } ?>
            <link><?php the_permalink_rss() ?></link>

            <<?php echo get_option( $_opt_custom_feed_field ); ?>>
            	<?php echo get_post_meta( wp_custom_feed_get_post_id(), $_opt_custom_feed_field, true); ?>
            </<?php echo get_option( $_opt_custom_feed_field ); ?>>
            <customData>
            	<?php echo get_post_meta( wp_custom_feed_get_post_id(), $_opt_custom_field_data, true); ?>
            </customData>
           
            <?php do_action('rss_item'); ?>
        </item>
                <?php endwhile; else: // End of WP Loop ?>
            <?php endif; // End of WP Loop ?>
    </channel>
</rss>

<?php } 

// This function returns the ID of the active post in the WP Loop
function wp_custom_feed_get_post_id() {
    global $wp_query;
    $postid = $wp_query->post->ID;
    return $postid;
}

// This function provides URL rewriting for custom feed names
function wp_custom_feed_rewrite_rules( $wp_rewrite ) {
    $new_rules = array(
        'feed/(.+)' => 'index.php?feed='.$wp_rewrite->preg_index(1)
    );
    $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}

// This is the primary function that registers the feed
function wp_custom_feed_add_feed() {
    global $wp_rewrite;

    // Set feed name to custom defined name or default feed name of custom
    // $customFeedName = get_option('wp_custom_feed_name');

    // Add the feed and associated function
    $feedName = get_option( 'wp_custom_feed_name' );
    add_feed( $feedName, 'wp_custom_feed_create_feed');
    add_action('generate_rewrite_rules', 'wp_custom_feed_rewrite_rules');
    $wp_rewrite->flush_rules();
}

// Setup the admin menu
function wp_custom_feed_admin_menu() {
    add_submenu_page('options-general.php','WP Custom Feed','WP Custom Feed',8,__FILE__,wp_custom_feed_admin_page) ;
}

// Setup WP Administrative pages
function wp_custom_feed_admin_page() {
	
	// Grab global varaibles
	global $_opt_custom_feed_name;
	global $_opt_custom_feed_field;
	global $_opt_custom_feed_enabled;
	global $_opt_custom_feed_data;
	
	$_hidden_field = 'wp_custom_hidden_field';

    // Read in existing option value from database
	$_val_custom_feed_name    = get_option($_opt_custom_feed_name);
	$_val_custom_feed_field   = get_option($_opt_custom_feed_field);
	$_val_custom_feed_enabled = get_option($_opt_custom_feed_enabled);
	$_val_custom_feed_data    = get_option($_opt_custom_feed_data);
	$_val_rss_excerpt_length  = get_option('rss_excerpt_length');

    // See if the user has posted us some information
    // If they did, this hidden field will be set to 'Y'
    if( $_POST[ $_hidden_field ] == 'Y' ) {

        // Read their posted value
		$_val_custom_feed_name    = $_POST[$_opt_custom_feed_name];
		$_val_custom_feed_field   = $_POST[$_opt_custom_feed_field];
		$_val_custom_feed_enabled = $_POST[$_opt_custom_feed_enabled];
		$_val_custom_feed_data    = $_POST[$_opt_custom_feed_data];
		$_val_rss_excerpt_length  = $_POST['rss_excerpt_length'];

        // Save the posted value in the database
        update_option( $_opt_custom_feed_name,    $_val_custom_feed_name);
        update_option( $_opt_custom_feed_field,   $_val_custom_feed_field);
        update_option( $_opt_custom_feed_enabled, $_val_custom_feed_enabled);
        update_option( $_opt_custom_feed_data,    $_val_custom_feed_data);
        update_option( 'rss_excerpt_length', $_val_rss_excerpt_length); 

        // Put an options updated message on the screen
        echo '<div class="updated"><p><strong>' . __('Options saved.') . '</strong></p></div>';
    }

    // Now display the options editing screen
    echo '<div class="wrap">';
    echo '<div class="icon32" id="icon-options-general"><br></div>';
    echo "<h2>" . __( 'WP Custom Feed Configuration' ) . "</h2>";

    // options form
    ?>

<form name="form1" method="post" action="<?php echo str_replace( '%7E', '~', $_SERVER['REQUEST_URI']); ?>">
    <input type="hidden" name="<?php echo $_hidden_field; ?>" value="Y">

	<p><?php _e( 'Custom Field Enabled?' ); ?>
		<input type="checkbox" name="<?php echo $_opt_custom_feed_enabled; ?>" value="1" <?php echo ($_val_custom_feed_enabled) ? 'Checked' : '' ; ?>>
	</p>	
    <p><?php _e( 'Name of Custom Feed:' ); ?>
        <input type="text" name="<?php echo $_opt_custom_feed_name; ?>" value="<?php echo $_val_custom_feed_name; ?>" size="20">
    </p>
    <p><?php _e( 'Name of Custom Field To Display:' ); ?>
        <input type="text" name="<?php echo $_opt_custom_feed_field; ?>" value="<?php echo $_val_custom_feed_field; ?>" size="20">
    </p>    
    <p><?php _e( 'Additional Custom Data To Display:' ); ?>
    	<input type="text" name="<?php echo $_opt_custom_feed_data; ?>" value="<?php echo $_val_custom_feed_data; ?>" size="50">
    </p>
    <p><?php _e( 'RSS Excerpt Length:' ); ?>
    	<input type="text" name="rss_excerpt_length" value="<?php echo $_val_rss_excerpt_length; ?>" size="20">
    </p>
    <p class="submit">
        <input class="button-primary" type="submit" name="Submit" value="<?php _e('Update Options' ) ?>" />
    </p>
    <hr/>
    <p><?php //$_rewrite_rules = get_option('rewrite_rules'); foreach ($_rewrite_rules as $rule) echo $rule . "<br>"?></p>

</form>
</div>

<?php


}

// set plugin variables
$_opt_custom_feed_name    = 'wp_custom_feed_name';
$_opt_custom_feed_field   = 'wp_custom_feed_field';
$_opt_custom_feed_enabled = 'wp_custom_feed_enabled';
$_opt_custom_feed_data    = 'wp_custom_feed_data';

// Hook into init action for RSS feed and admin_menu action for admin menus
$opt_val_feed_name 	  = get_option( $_opt_custom_feed_name );
if (!$opt_val_feed_name) {
	add_option( $_opt_custom_feed_name, 'custom');
};
$opt_val_feed_enabled = get_option( $_opt_custom_feed_enabled );
if ($opt_val_feed_enabled = 'Yes') { 
	add_action('init', 'wp_custom_feed_add_feed'); 
};
add_action('admin_menu', 'wp_custom_feed_admin_menu');

?>
